#!/bin/bash
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

case $1 in
        "ovn-nb") /usr/share/ovn/scripts/ovn-ctl run_nb_ovsdb
        ;;
        "ovn-sb") /usr/share/ovn/scripts/ovn-ctl run_sb_ovsdb
        ;;
        "ovn-northd") ovn-northd --pidfile \
                      --ovnnb-db="unix:/var/run/ovn/ovnnb_db.sock" \
                      --ovnsb-db="unix:/var/run/ovn/ovnsb_db.sock" \
                      --log-file=/var/log/ovn/ovn-northd.log
        ;;
        "ovn-northd-cluster") ovn-northd --pidfile \
                              --ovnnb-db=$OVN_NB_DB \
                              --ovnsb-db=$OVN_SB_DB \
                              --log-file=/var/log/ovn/ovn-northd.log
        ;;
        "ovn-nb-tcp") source /etc/ovn/ovn_default_nb_port
                      /usr/share/ovn/scripts/ovn-ctl start_ovsdb
                      ovn-nbctl set-connection ptcp:$nb_db_port
                      /usr/share/ovn/scripts/ovn-ctl stop_ovsdb
                      /usr/share/ovn/scripts/ovn-ctl run_nb_ovsdb
        ;;
        "ovn-nb-cluster-create") /usr/share/ovn/scripts/ovn-ctl \
                                 --db-nb-addr=$host_ip \
                                 --db-nb-cluster-local-addr=$host_ip \
                                 start_nb_ovsdb
                                 ovn-nbctl set-connection ptcp:$nb_db_port
                                 /usr/share/ovn/scripts/ovn-ctl stop_nb_ovsdb
                                 /usr/share/ovn/scripts/ovn-ctl \
                                 --db-nb-addr=$host_ip \
                                 --db-nb-cluster-local-addr=$host_ip \
                                 run_nb_ovsdb
        ;;
        "ovn-nb-cluster-join") /usr/share/ovn/scripts/ovn-ctl \
                               --db-nb-addr=$host_ip \
                               --db-nb-cluster-local-addr=$host_ip \
                               --db-nb-cluster-remote-addr=$remote_host \
                               run_nb_ovsdb
        ;;
        "ovn-sb-tcp") source /etc/ovn/ovn_default_sb_port
                      /usr/share/ovn/scripts/ovn-ctl start_ovsdb
                      ovn-sbctl set-connection ptcp:$sb_db_port
                      /usr/share/ovn/scripts/ovn-ctl stop_ovsdb
                      /usr/share/ovn/scripts/ovn-ctl run_sb_ovsdb
        ;;
        "ovn-northd-tcp") source /etc/ovn/ovn_default_northd_host
                          source /etc/ovn/ovn_default_nb_port
                          source /etc/ovn/ovn_default_sb_port
                          ovn-northd --pidfile \
                          --ovnnb-db="tcp:$northd_host:$nb_db_port" \
                          --ovnsb-db="tcp:$northd_host:$sb_db_port" \
                          --log-file=/var/log/ovn/ovn-northd.log
        ;;
        "ovn-sb-cluster-create") /usr/share/ovn/scripts/ovn-ctl \
                                 --db-sb-addr=$host_ip \
                                 --db-sb-cluster-local-addr=$host_ip \
                                 start_sb_ovsdb
                                 ovn-sbctl set-connection ptcp:$sb_db_port
                                 /usr/share/ovn/scripts/ovn-ctl stop_sb_ovsdb
                                 /usr/share/ovn/scripts/ovn-ctl \
                                 --db-sb-addr=$host_ip \
                                 --db-sb-cluster-local-addr=$host_ip \
                                 run_sb_ovsdb
        ;;
        "ovn-sb-cluster-join") /usr/share/ovn/scripts/ovn-ctl \
                               --db-sb-addr=$host_ip \
                               --db-sb-cluster-local-addr=$host_ip \
                               --db-sb-cluster-remote-addr=$remote_host \
                               run_sb_ovsdb
        ;;
        "ovn-controller") ovn-controller --pidfile \
                          --log-file=/var/log/ovn/ovn-controller.log
        ;;
        *) echo "$0 [ovn-nb-tcp|ovn-sb-tcp|ovn-northd-tcp|ovn-controller
                     |ovn-nb-cluster-create|ovn-nb-cluster-join
                     |ovn-sb-cluster-create|ovn-sb-cluster-join
                     |ovn-northd-cluster]"
esac